import pygame
import random

class Fruit:
    def __init__(self, screen_width, screen_height):
        self.screen_width = screen_width
        self.screen_height = screen_height
        
        # 随机属性
        self.radius = random.randint(20, 30)
        self.x = random.randint(self.radius, screen_width - self.radius)
        self.y = screen_height
        self.vel_x = random.uniform(-3, 3)
        self.vel_y = random.uniform(-15, -10)
        self.gravity = 0.3
        self.cut = False
        
        # 随机颜色（模拟不同水果）
        self.colors = [
            (255, 0, 0),    # 红色（苹果）
            (255, 165, 0),  # 橙色（橙子）
            (255, 255, 0),  # 黄色（柠檬）
            (0, 255, 0),    # 绿色（西瓜）
            (128, 0, 128)   # 紫色（葡萄）
        ]
        self.color = random.choice(self.colors)
        
        # 碰撞矩形
        self.rect = pygame.Rect(
            self.x - self.radius,
            self.y - self.radius,
            self.radius * 2,
            self.radius * 2
        )

    def update(self):
        # 物理运动
        self.vel_y += self.gravity
        self.x += self.vel_x
        self.y += self.vel_y
        
        # 更新碰撞矩形
        self.rect.center = (self.x, self.y)
        
        # 边界反弹
        if self.x - self.radius < 0 or self.x + self.radius > self.screen_width:
            self.vel_x *= -0.8

    def draw(self, screen):
        if self.cut:
            # 绘制被切开的水果（两个半圆）
            pygame.draw.circle(screen, self.color, 
                              (int(self.x - 5), int(self.y)), self.radius - 2)
            pygame.draw.circle(screen, self.color, 
                              (int(self.x + 5), int(self.y)), self.radius - 2)
        else:
            # 绘制完整水果
            pygame.draw.circle(screen, self.color, (int(self.x), int(self.y)), self.radius)
            pygame.draw.circle(screen, (255, 255, 255), 
                              (int(self.x) - 5, int(self.y) - 5), 3)  # 高光